﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using CNFM_CROCODILE_LOGO.Core.Domain.ViewModels;
using CNFM_CROCODILE_LOGO.Services.Interfaces;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace CNFM_CROCODILE_LOGO_Web.Controllers
{
    /// <summary>
    /// 用户管理
    /// </summary>
    public class UserController : Controller
    {
        private IBasicServices _pro;
        public UserController(IBasicServices pro)
        {
            this._pro = pro;
        }

        /// <summary>
        /// 用户列表
        /// </summary>
        /// <returns></returns>
        public IActionResult List()
        {
            return View();
        }
        /// <summary>
        /// 用户新增
        /// </summary>
        /// <returns></returns>
        public IActionResult Add()
        {
            return View();
        }
        /// <summary>
        /// 用户修改
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IActionResult Edit(string id)
        {
            var result = _pro.GetCrocodileUser(id);
            var json = JsonConvert.SerializeObject(result.Result);
            JObject obj = JObject.Parse(json);
            CrocodileUserModels entity = JsonConvert.DeserializeObject<CrocodileUserModels>(obj["Data"].ToString());

            return View(entity);
        }

        /// <summary>
        /// 企业开户
        /// </summary>
        /// <returns></returns>
        public IActionResult EnterpriseAdd()
        {
            return View();
        }

        /// <summary>
        /// 用户授权
        /// </summary>
        /// <returns></returns>
        public IActionResult Authorization(string id)
        {
            ViewData["Id"] = id;
            return View();
        }

        /// <summary>
        /// 用户列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="areUnCode"></param>
        /// <param name="type"></param>
        /// <param name="sasearch"></param>
        /// <returns></returns>
        [Authorize("Permission")]
        public JsonResult GetCrocodileUserAll(int index, int size, string areUnCode, string type, string sasearch)
        {
            //var auth = HttpContext.AuthenticateAsync().Result.Principal.Claims;
            //var userName = auth.FirstOrDefault(t => t.Type.Equals(ClaimTypes.Name))?.Value;
            //var role = auth.FirstOrDefault(t => t.Type.Equals("Role"))?.Value;
            //// var userId = (User.Identity as ClaimsIdentity).Claims.FirstOrDefault(p => p.Type == "Id").Value;
            //var userNames = (User.Identity as ClaimsIdentity).Claims.FirstOrDefault(t => t.Type.Equals(ClaimTypes.Name))?.Value;
            var ret = _pro.GetCrocodileUserAll(index, size, areUnCode, type, sasearch);
            return Json(ret);
        }

        /// <summary>
        /// 用户新增修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddUpdateCrocodileUser([FromBody] CrocodileUserModels model)
        {
            var ret = _pro.AddUpdateCrocodileUser(model);
            return Json(ret);
        }

        /// <summary>
        /// 企业开户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddEnterprise([FromBody]CrocodileUserModels model)
        {
            var ret = _pro.AddEnterprise(model);
            return Json(ret);
        }

        /// <summary>
        /// 用户停用
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpDelete]
        public JsonResult DelectCrocodileUser(string userId)
        {
            var ret = _pro.DelectCrocodileUser(userId);
            return Json(ret);
        }

        /// <summary>
        /// 用户授权
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="roId"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UserAuthorization(string userId, string roId)
        {
            var ret = _pro.UserAuthorization(userId, roId);
            return Json(ret);
        }

        /// <summary>
        /// 地区树
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public JsonResult GetAreaTreeAll(string code)
        {
            var ret = _pro.GetAreaTreeAll(code);
            return Json(ret);
        }
    }
}